package deepbrain.simnetwork.mechanism;

import java.util.List;

import deepbrain.simnetwork.network.Network;
import deepbrain.simnetwork.network.NetworkState;
import deepbrain.simnetwork.network.NodeState;

/**
 * 
 * 
 * @author Li Yang
 * @create 2009-2-25
 */
public abstract class GenericMechanism implements Mechanism {

	protected MechanismProperties properties;
	
	public void initialize(MechanismProperties properties) {
		this.properties = properties;
	}
	
	/**
	 * Network should have a mechanism, and each node can have its own
	 * mechanism. If one node's mechanism is null, use network's mechanism
	 * instead.
	 */
	public NetworkState process(Network network, NetworkState state,
			List<Integer> subset) {
		int numOfNodes = state.numOfNodes();
		NodeState[] nextStates = new NodeState[numOfNodes];
		NodeState resultState = null;
		if (subset == null) {
			for (int i = 0; i < numOfNodes; i++) {
				if (network.getMechanism(i) == null)
					resultState = nextNodeState(i, network, state);
				else
					resultState = network.getMechanism(i).nextNodeState(i,
							network, state);
				nextStates[i] = resultState;
			}
		} else {
			for (int i : subset) {
				if (network.getMechanism(i) == null)
					resultState = nextNodeState(i, network, state);
				else
					resultState = network.getMechanism(i).nextNodeState(i,
							network, state);
				nextStates[i] = resultState;
			}
		}
		return new NetworkState(nextStates);
	}

}
